package com.library.Conrtoller;

import com.library.Entity.Message;
import com.library.Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入表单的用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

//        System.out.print(username);
//        System.out.print(password);


        //判断是否登录成功
        Message usermessage = UserService.userlogin(username,password);

        if(usermessage.getCode()==1){
            //成功,则重定向跳转首页
            request.setAttribute("user",usermessage.getObject());
            request.getSession().setAttribute("userSeccess",usermessage.getObject());
            response.sendRedirect("library.jsp");
        }
        else{
            //失败，则请求转发登录界面
            request.setAttribute("usermessage",usermessage);
            request.getRequestDispatcher("user.jsp").forward(request,response);
        }
    }
}
